package actions;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;

import service.AppDicService;
import domain.AppDic;
import domain.AppDicRecord;

/**
 * @author luoy
 * 
 */
public class AppDicMainAction extends DispatchAction {

    private AppDicService appDicService;

    /**
     * 显示所有的AppDic信息
     * @param mapping aciton mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward displayAllAppDic(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        Collection<AppDic> allAutoGenDic = appDicService.getAllAppDic();
        request.setAttribute("AUTO_GEN_DIC_LIST", allAutoGenDic);
        return mapping.findForward("main");
    }

    /**
     * 显示所有的AppDicRecord信息
     * @param mapping aciton mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward displayAllAppDicRec(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String dicName = request.getParameter("dicName");
        Collection<AppDicRecord> allAutoRecords = appDicService
                .getAppDicRecordByDicName(dicName);
        request.setAttribute("AutoGenRecordsLst", allAutoRecords);
        return mapping.findForward("recordmain");
    }

    /**
     * @param mapping aciton mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward deleteAppDic(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String dicName = request.getParameter("dicName");
        appDicService.deleteAppDic(dicName);
        return mapping.findForward("appdiclist");
    }

    /**
     * 根据dicName得到对应的所有记录
     * 
     * @param mapping aciton mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward getAllAutoRecordByDicName(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String dicName = request.getParameter("dicName");
        String dicTitle = (String) request.getAttribute("dicTitle");
        Collection<AppDicRecord> allAutoRecords = appDicService
                .getAppDicRecordByDicName(dicName);

        request.setAttribute("AutoGenRecordsLst", allAutoRecords);
        request.setAttribute("dicTitle", dicTitle);
        return mapping.findForward("main");
    }

    /**
     * 修改 AppDic
     * @param mapping aciton mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward dispUpdateAppDicScreen(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String dicName = request.getParameter("dicName");
        AppDic appdic = appDicService.getAppDicByDicName(dicName);
        request.setAttribute("dicName", appdic.getDicName());
        request.setAttribute("dicTitle", appdic.getDicTitle());
        request.setAttribute("dicDesc", appdic.getDicDesc());
        return mapping.findForward("AppDicUpdate.screen");
    }

    /**
     * 修改 AppDicReocrd
     * @param mapping aciton mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward dispUpdateAppDicRecScreen(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        AppDicRecord rec = appDicService.getAutoGenDicRecordById(Integer
                .valueOf(id));

        request.setAttribute("recKey", rec.getRecKey());
        request.setAttribute("recValue", rec.getRecValue());
        request.setAttribute("recDesc", rec.getRecDesc());
        return mapping.findForward("AppDicRecordUpdate.screen");
    }

    /**
     * 修改 AppDic
     * @param mapping aciton mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward doUpdateAppDic(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String dicName = request.getParameter("dicName");
        String dicTitle = request.getParameter("dicTitle");
        String dicDesc = request.getParameter("dicDesc");
        ActionErrors errors = new ActionErrors();
        if (dicName == null || "".equals(dicName)) {
            errors.add("dicName", new ActionMessage("errors.required", ""));
        }
        if (dicTitle == null || "".equals(dicTitle)) {
            errors.add("dicTitle", new ActionMessage("errors.required", ""));
        }
        if (!errors.isEmpty()) {
            this.addErrors(request, errors);
            return mapping.findForward("AppDicUpdate.screen");
        }
        AppDic existAppDic = null;
        Collection<AppDic> autoGenDicLst = appDicService.getAllAppDic();
        for (AppDic appDic : autoGenDicLst) {
            if (appDic.getDicName().equals(dicName)) {
                existAppDic = appDic;
                continue;
            }
            if (dicName.equals(appDic.getDicName())
                    || dicTitle.equals(appDic.getDicTitle())) {
                errors.add("info.autoGenDic.exist", new ActionMessage(
                        "info.autoGenDic.exist"));
                this.addErrors(request, errors);
                return mapping.findForward("AppDicUpdate.screen");
            }
        }
        existAppDic.setDicDesc(dicDesc);
        existAppDic.setDicName(dicName);
        existAppDic.setDicTitle(dicTitle);
        appDicService.updateAppDic(existAppDic);
        return mapping.findForward("appdiclist");

    }

    /**
     * 创建 AppDic
     * @param mapping aciton mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward doCreateAppDic(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String dicName = request.getParameter("dicName");
        String dicTitle = request.getParameter("dicTitle");
        String dicDesc = request.getParameter("dicDesc");
        ActionErrors errors = new ActionErrors();

        if (dicName == null || "".equals(dicName)) {
            errors.add("dicName", new ActionMessage("errors.required", ""));
        }
        if (dicTitle == null || "".equals(dicTitle)) {
            errors.add("dicTitle", new ActionMessage("errors.required", ""));
        }
        if (!errors.isEmpty()) {
            this.addErrors(request, errors);
            return mapping.findForward("AppDicCreate.screen");
        }

        Collection<AppDic> allDics = appDicService.getAllAppDic();
        for (AppDic appDic : allDics) {
            if (dicName.equals(appDic.getDicName())
                    || dicTitle.equals(appDic.getDicTitle())) {
                errors.add("info.autoGenDic.exist", new ActionMessage(
                        "info.autoGenDic.exist"));
                this.addErrors(request, errors);
                return mapping.findForward("AppDicCreate.screen");
            }
        }
        AppDic newAppdic = new AppDic();
        newAppdic.setDicDesc(dicDesc);
        newAppdic.setDicName(dicName);
        newAppdic.setDicTitle(dicTitle);
        appDicService.createAppDic(newAppdic);
        return mapping.findForward("appdiclist");

    }

    /**
     * 更新字典表记录
     * @param mapping aciton mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward doUpdateAppDicRec(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String idString = request.getParameter("id");
        Integer id = Integer.parseInt(idString);
        String dicName = request.getParameter("dicName");
        String recKey = request.getParameter("recKey");
        String recValue = request.getParameter("recValue");
        String recDesc = request.getParameter("recDesc");
        ActionErrors errors = new ActionErrors();
        if (recKey == null || "".equals(recKey)) {
            errors.add("recKey", new ActionMessage("errors.required", ""));
        }
        if (recValue == null || "".equals(recValue)) {
            errors.add("recValue", new ActionMessage("errors.required", ""));
        }
        if (!errors.isEmpty()) {
            this.addErrors(request, errors);
            return mapping.findForward("AppDicRecordUpdate.screen");
        }
        Collection<AppDicRecord> recordLst = appDicService
                .getAppDicRecordByDicName(dicName);
        AppDicRecord existRecord = null;
        for (AppDicRecord record : recordLst) {
            if (record.getId().equals(id)) {
                existRecord = record;
                continue;
            }
            if (recKey.equals(record.getRecKey())
                    || recValue.equals(record.getRecValue())) {
                errors = new ActionErrors();
                errors.add("info.autoGenDicRecord.exist", new ActionMessage(
                        "info.autoGenDicRecord.exist"));
                this.addErrors(request, errors);
                return mapping.findForward("AppDicRecordUpdate.screen");
            }
        }

        existRecord.setDicName(dicName);
        existRecord.setRecKey(recKey);
        existRecord.setRecValue(recValue);
        existRecord.setRecDesc(recDesc);
        appDicService.updateAutoGenDic(existRecord);
        return mapping.findForward("dicrecordlist");
    }

    /**
     * 创建新的字典表记录
     * @param mapping aciton mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward doCreateAppDicRec(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String dicName = request.getParameter("dicName");
        String recKey = request.getParameter("recKey");
        String recValue = request.getParameter("recValue");
        String recDesc = request.getParameter("recDesc");
        ActionErrors errors = new ActionErrors();

        if (recKey == null || "".equals(recKey)) {
            errors.add("recKey", new ActionMessage("errors.required", ""));
        }
        if (recValue == null || "".equals(recValue)) {
            errors.add("recValue", new ActionMessage("errors.required", ""));
        }
        if (!errors.isEmpty()) {
            this.addErrors(request, errors);
            return mapping.findForward("AppDicRecordCreate.screen");
        }
        Collection<AppDicRecord> recordLst = appDicService
                .getAppDicRecordByDicName(dicName);
        for (AppDicRecord record : recordLst) {

            if (recKey.equals(record.getRecKey())
                    || recValue.equals(record.getRecValue())) {
                errors.add("info.autoGenDicRecord.exist", new ActionMessage(
                        "info.autoGenDicRecord.exist"));
                this.addErrors(request, errors);
                return mapping.findForward("AppDicRecordCreate.screen");
            }
        }

        AppDicRecord newAutoGenDicRecord = new AppDicRecord();
        newAutoGenDicRecord.setDicName(dicName);
        newAutoGenDicRecord.setRecKey(recKey);
        newAutoGenDicRecord.setRecValue(recValue);
        newAutoGenDicRecord.setRecDesc(recDesc);
        appDicService.createAutoGenDicRecord(newAutoGenDicRecord);

        return mapping.findForward("dicrecordlist");
    }

    /**
     * 删除AutoGenDicRecord
     * @param mapping aciton mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward deleteAppDicRec(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        appDicService.deleteAutoGenDicRecordById(Integer.valueOf(id));
        return mapping.findForward("dicrecordlist");
    }

    /**
     * @param pAppDicService the appDicService to set
     */
    public void setAppDicService(AppDicService pAppDicService) {
        appDicService = pAppDicService;
    }

}
